package com.ht.impl.myactions;

import com.ht.aems.swing.util.MyAction;
import com.ht.impl.ResourceTypeCons;
import com.ht.impl.dialogs.MeasurePointChooser;
import com.mxgraph.model.mxCell;
import com.mxgraph.util.mxUtils;
import com.mxgraph.swing.mxGraphComponent;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.awt.event.ActionEvent;

/**
 * Created by IntelliJ IDEA.
 *
 * @author Dong Shufeng
 *         Date: 2010-7-2
 */
public class SetMeasure extends MyAction implements ResourceTypeCons {
    public void actionPerformed(ActionEvent e) {
        mxGraphComponent component = (mxGraphComponent) e.getSource();
        Object[] cells = component.getGraph().getSelectionCells();
        if (cells.length == 0 || cells.length > 1)
            return;
        mxCell cell = (mxCell) cells[0];
        Document doc = mxUtils.createDocument();
        Element element = null;
        MeasurePointChooser measChooser = new MeasurePointChooser();
        if (cell.getValue() != null && cell.getValue() instanceof Element) {
            element = (Element) cell.getValue();
            measChooser.setShowTypeValue("TYPE=" + element.getAttribute(RESOURCE_TYPE));
            measChooser.setShowIdValue("ID=" + element.getAttribute(RESOURCE_ID));
        }
        measChooser.setModal(true);
        measChooser.setVisible(true);
        if (measChooser.getSelectedTypeItem().equals(""))
            return;
        if (measChooser.getOptionName().equals(""))
            return;
        if (measChooser.getSelectedId().equals(""))
            return;
        if (element == null) {
            element = doc.createElement("Element");
            element.setTextContent(cell.getValue() == null ? "null" : cell.getValue().toString());
            cell.setValue(element);
        }
        element.setAttribute(RESOURCE_TYPE, measChooser.getSelectedTypeItem());
        element.setAttribute(RESOURCE_ID, measChooser.getSelectedId());
    }
}
